package org.jeecg.modules.storage.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.inventory.entity.BussInventoryDetails;
import org.jeecg.modules.inventory.mapper.BussInventoryDetailsMapper;
import org.jeecg.modules.inventory.service.IBussInventoryDetailsService;
import org.jeecg.modules.storage.entity.BussDeductGoods;
import org.jeecg.modules.storage.mapper.BussDeductGoodsMapper;
import org.jeecg.modules.storage.service.IBussDeductGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 扣货表
 * @Author: jeecg-boot
 * @Date:   2024-08-08
 * @Version: V1.0
 */
@Service
public class BussDeductGoodsServiceImpl extends ServiceImpl<BussDeductGoodsMapper, BussDeductGoods> implements IBussDeductGoodsService {

    @Autowired
    private BussInventoryDetailsMapper bussInventoryDetailsMapper;
    @Autowired
    private IBussInventoryDetailsService iBussInventoryDetailsService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveMain(BussDeductGoods bussDeductGoods) { // quantity
        BussInventoryDetails bussInventoryDetails = bussInventoryDetailsMapper.selectById(bussDeductGoods.getInventoryDetailId());
        if(bussInventoryDetails.getQuantity().compareTo(bussDeductGoods.getNum()) < 0){
            throw new RuntimeException("库存不足") ;
        }
        bussInventoryDetails.setOccupationQuantity(bussInventoryDetails.getOccupationQuantity().add(bussDeductGoods.getNum()));
        bussInventoryDetails.setStatus("3");
        bussInventoryDetailsMapper.updateById(bussInventoryDetails);
        this.save(bussDeductGoods);
    }

    /**
     * 自动扣货
     */
    @Override
    public void autoDeductGoods(){
        String yesterday = DateUtil.format(DateUtil.yesterday(), "yyyy-MM-dd");
        String daybeforeyesterday = DateUtil.format(DateUtil.offsetDay(new Date(),-2), "yyyy-MM-dd");
        //.between(BussInventoryDetails::getRestrictedDate, daybeforeyesterday, yesterday)
        Long count = bussInventoryDetailsMapper.selectCount(new LambdaQueryWrapper<BussInventoryDetails>()
                        .le(BussInventoryDetails::getRestrictedDate, yesterday)
                .eq(BussInventoryDetails::getIsAutodeductgoods, 0)
                        .eq(BussInventoryDetails::getQualityInspectionStatus, 1 )
                .orderByDesc(BussInventoryDetails::getId));
        if(count > 0){
            int totalPages = (int) Math.ceil((double) count / 1000l);
            List<BussInventoryDetails> recordList = new ArrayList<>();
            List<BussDeductGoods> goodsList = new ArrayList<>();
            for (int i = 0; i < totalPages; i++) {
                //分页查询优化数据量大时查询效率
                Page<BussInventoryDetails> bussInventoryDetailsPage = bussInventoryDetailsMapper.selectPage(new Page<>(i, 1000), new LambdaQueryWrapper<BussInventoryDetails>()
                        .le(BussInventoryDetails::getRestrictedDate, yesterday)
                        .eq(BussInventoryDetails::getIsAutodeductgoods, 0)
                        .eq(BussInventoryDetails::getQualityInspectionStatus, 1 )
                        .orderByDesc(BussInventoryDetails::getId));
                List<BussInventoryDetails> records = bussInventoryDetailsPage.getRecords();
                for (BussInventoryDetails record : records) {
                    BigDecimal quantity = record.getQuantity();
                    record.setOccupationQuantity(record.getOccupationQuantity().add(quantity));
                    record.setIsAutodeductgoods(1);
                    recordList.add(record);
                    BussDeductGoods bussDeductGoods = new BussDeductGoods();
                    bussDeductGoods.setNum(quantity);
                    bussDeductGoods.setInventoryDetailId(record.getId());
                    goodsList.add(bussDeductGoods);
                }
            }
            iBussInventoryDetailsService.saveOrUpdateBatch(recordList);
            this.saveOrUpdateBatch(goodsList);

        }
    }




}
